package com.apes.fn.scm.commodityInquiry.model;

import com.apes.fn.mdm.purchase.PurchasingGroup;
import com.apes.fn.tms.model.Site;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Brand;
import com.apes.scm.masterdata.product.model.Category;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CommodityInquiryItem.CONST_TABLE_NAME)
public class CommodityInquiryItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "PUR_COMMODITY_INQUIRY_ITEM";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name="商品询价明细", prefix = "1")
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "commodityInquiryOrder_id", referencedColumnName = "id")
    @Comment("询价单号")
    private CommodityInquiryOrder commodityInquiryOrderId;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品编码")
    private Product productId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fitting", referencedColumnName = "id")
    @Comment("配件")
    private Category fitting;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brand_id", referencedColumnName = "id")
    @Comment("品牌编码")
    private Brand brandId;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom productUomId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "original_product_id", referencedColumnName = "id")
    @Comment("原商品编码")
    private Product originalProductId;

//    @Column(name = "sales_number", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
//    @Comment("销售件数")
//    private double salesNumber;

    @Column(name = "sales_volume", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("销售数量")
    private double salesVolume;

    @Column(name = "deliverables", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("可交货件数")
    private double deliverables;

    @Column(name = "purchase_prices", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("采购单价")
    private double purchasePrices;

    @Column(name = "unit_price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售单价")
    private double unitPrice;

    @Column(name = "sale_limit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售限价")
    private double saleLimit;

    @Column(name = "supplier_unit_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("供应商询价单销售单价")
    private double supplierUnitPrice; // 供应商询价单销售单价（销售单价1）

    @Column(name = "down_company_puchase_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("下游公司采购价")
    private double downCompanyPurchasePrice;

    @Column(name = "down_company_unit_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("下游公司销售价")
    private double downCompayUnitPrice;

    @Column(name = "purchase4S_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("采购4S价格")
    private double purchase4SPrice;

    @Column(name = "unit4s_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售4S价格")
    private double unit4SPrice;

    @Column(name = "empowered_purchase_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("赋能采购价")
    private double empoweredPurchasePrice;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supply_staus", referencedColumnName = "id")
    @Comment("货源状态")
    private Enumeration supplyStatus;

    @Column(name = "price_validity", columnDefinition = "number(2)")
    @Comment("价格有效期（天）")
    private int priceValidity;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商编码")
    private PartyRole supplierId;

    @Column(name = "oe_description")
    @Comment("OE描述")
    private String oeDescription;

    @Column(name = "purchase_opinion")
    @Comment("采购意见")
    private String purchaseOpinion;

    @Column(name = "supplier_notes")
    @Comment("供应商备注")
    private String supplierNotes;

    @Column(name = "summary")
    @Comment("摘要")
    private String summary;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole companyId;

    @Column(name = "inquiry_mark", columnDefinition = "number(2) default 0")
    @Comment("询价标识(0:原;1:替)")
    private int inquiryMark;

    @Column(name = "secondary_inquiry", columnDefinition = "number(2) default 0")
    @Comment("二次询价(0:否;1:是)")
    private int secondaryInquiry;

    @Column(name = "inquiry_type", columnDefinition = "number(2) default 1")
    @Comment("询价类型(0:库存;1:价格)")
    private int inquiryType;

    @Column(name = "map_address", length = 4000)
    @Comment("图片地址")
    private String mapAddress;

    @Column(name = "part_code", length = 4000)
    @Comment("零件编码")
    private String partCode;

    @Column(name = "part_name", length = 4000)
    @Comment("零件名称")
    private String partName;

    @Column(name = "image_sn")
    @Comment("图号")
    private String imageSN;

    @Transient
    private String PJMC;

    @Column(name = "eta")
    @Comment("预计送达时间")
    private Date eta;

    @Column(name = "arrival_time")
    @Comment("到达时长")
    private int arrivalTime = 0;

    @Column(name = "un_tax_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("无税参考价")
    private double unTaxPrice = 0;

    @Column(name = "supplier_amt", columnDefinition = ApesConst.FIELD_MONEY_16_4)
    @Comment("税率")
    private double supplierAmt = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "purchasing_group_id", referencedColumnName = "id")
    @Comment("采购组")
    private PurchasingGroup purchasingGroup;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "site_id", referencedColumnName = "id")
    @Comment("站点编码")
    private Site site;

    @Column(name = "site_shipping_address")
    @Comment("站点收货地址")
    private String siteShippingAddress;


    /**
     * 换算销售数量
     * @return
     */
    public double getSalesVolume(double salesVolume, ProductUom productUom) {
        if (StringUtils.isEmpty(this.salesVolume)) {
            this.salesVolume = salesVolume;
            this.productUomId = productUom;
        }
        return productUomId.computeQty(salesVolume, productUomId, false);
    }


}



















